//
//  BookShelfViewController.m
//  GenericProject
//
//  Created by 大鹏 on 2022/7/29.
//  Copyright © 2022 漠然丶情到深处. All rights reserved.
//

#import "BookShelfViewController.h"
#import "TransmissionViewController.h"
#import "BookShelfCollectionViewCell.h"

@interface BookShelfViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property(nonatomic,strong)NSMutableArray *listArray;
@property(nonatomic,strong)UICollectionView *mainCollectionView;

@end

@implementation BookShelfViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    LSAddNotification(upDateList:, @"upDateList");
    // Do any additional setup after loading the view.
    [self setNavigation];
    [self analysis];
    [self createSubViews];
}

- (void)setNavigation {
    self.navView.isShowLeftButton = NO;
    self.navView.titleLabelText = @"书架";
    self.navView.isShowRightButton = YES;
    self.navView.rightButtonTitle = @"";
    self.navView.rightButtonImage = @"设置黑";
    self.navView.isShowNavViewLine = NO;
    LSWeakSelf(self);
    self.navView.RightActionBlock = ^{
        LSStrongSelf(self);
        TransmissionViewController *TransmissionVC = [[TransmissionViewController alloc]init];
        [self.navigationController pushViewController:TransmissionVC animated:YES];
    };
}

- (void)analysis {
    self.listArray = [NSMutableArray array];
    
    NSString *string = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    self.listArray = [NSMutableArray arrayWithArray:[fileManager contentsOfDirectoryAtPath:string error:nil]];
    
    for (NSString *str in self.listArray) {
        if ([str isEqualToString:@"DZMeBookRead"]) {
            [self.listArray removeObject:str];
            break;
        }
    }
    
    [self.mainCollectionView reloadData];
}

- (void)createSubViews {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    self.mainCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, LSNAVIGATION_BAR_HEIGHT, LSScreenWidth, LSScreenHeight - LSNAVIGATION_BAR_HEIGHT - LSHOME_INDICATOR_HEIGHT) collectionViewLayout:layout];
    self.mainCollectionView.backgroundColor = [UIColor lsLigthColor:[UIColor ls_colorWithHexString:@"F8F8F8" Alpha:1] darkColor:LSBlackColor];
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
    self.mainCollectionView.showsVerticalScrollIndicator = NO;
    [self.mainCollectionView registerClass:[BookShelfCollectionViewCell class] forCellWithReuseIdentifier:@"BookShelfCollectionViewCell"];
    [self.view addSubview:self.mainCollectionView];
    if (@available(iOS 11.0, *)) {
        self.mainCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.listArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BookShelfCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BookShelfCollectionViewCell" forIndexPath:indexPath];
   
    cell.bookNameString = self.listArray[indexPath.row];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [SVProgressHUD showWithStatus:@"加载中..."];
    [SVProgressHUD setDefaultStyle:SVProgressHUDStyleDark];
    [SVProgressHUD setDefaultAnimationType:SVProgressHUDAnimationTypeNative];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeGradient];
    
    NSString *string = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath = [string stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",self.listArray[indexPath.row]]];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"file:///private%@",[filePath stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<>"]]]];

    [DZMReadTextParser parserWithUrl:url completion:^(DZMReadModel * _Nullable readModel) {
        [SVProgressHUD dismiss];
        DZMReadController *DZMReadVC = [[DZMReadController alloc]init];
        DZMReadVC.readModel = readModel;
        [self.navigationController pushViewController:DZMReadVC animated:YES];
    }];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((LSScreenWidth - LSSYRealValue(64)) / 3, LSSYRealValue(150));
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(LSSYRealValue(16), LSSYRealValue(16) ,LSSYRealValue(16), LSSYRealValue(16));
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return LSSYRealValue(16);
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return LSSYRealValue(16);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeZero;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    return CGSizeZero;
}

- (void)upDateList:(NSNotification *)notif {
    [self.listArray removeAllObjects];
    
    NSString *string = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    self.listArray = [NSMutableArray arrayWithArray:[fileManager contentsOfDirectoryAtPath:string error:nil]];
    
    for (NSString *str in self.listArray) {
        if ([str isEqualToString:@"DZMeBookRead"]) {
            [self.listArray removeObject:str];
            break;
        }
    }
    
    [self.mainCollectionView reloadData];
}

- (void)dealloc {
    LSRemoveNotification(@"upDateList");
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
